﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace landlord2lib
{
    public class CardGroupBoom : CardGroup
    {
        public CardGroupBoom(Card[] cards)
        {
            m_cards = cards;
        }

        public override bool IsValid()
        {
            try
            {
                if (m_cards.Length < 4) return false;
                foreach (Card card in m_cards) card.Reset();

				if (IsMaxBoom()) return true;

                Card cardReal = null;
                foreach (Card card in m_cards)
                {
                    if (!card.IsJoker)
                    {
                        cardReal = card;
                        break;
                    }
                }
                if (cardReal != null)
                {
                    foreach (Card card in m_cards)
                    {
                        if (card.IsJoker) card.ComparePoint = cardReal.ComparePoint;
                    }
                }

                Card lastCard = null;
                foreach (Card card in m_cards)
                {
                    if (lastCard == null) lastCard = card;
                    else
                    {
                        if (lastCard.ComparePoint != card.ComparePoint) return false;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public override int CompareTo(CardGroup other)
        {
            try
            {
                if (other is CardGroupBoom)
                {
                    CardGroupBoom o = (CardGroupBoom)other;
					if (IsMaxBoom()) return 1;
                    if (m_cards.Length > o.m_cards.Length) return 1;
                    if (m_cards.Length < o.m_cards.Length) return -1;
                    return m_cards[0].CompareTo(o.m_cards[0]);
                }
                else return 1;
            }
            catch
            {
                return 0;
            }
        }

        private bool IsMaxBoom()
        {
            if (m_cards.Length == 4)
            {
                bool allJoker = true;
                foreach (Card card in m_cards)
                {
                    if (!card.IsJoker)
                    {
                        allJoker = false;
                        break;
                    }
                }
                if (allJoker) return true;
            }
            return false;
        }

        public static HintCardCollection Hint(Card[] handCards, CardGroupBoom lastGroup)
        {
            HintCardCollection result = new HintCardCollection();
            try
            {
                if (lastGroup != null && lastGroup.IsMaxBoom()) return result;
                CardStack stack = new CardStack(handCards);
                int minCount = lastGroup != null ? lastGroup.m_cards.Length : 4;
                int minPt = lastGroup == null ? (int)CardPoint.Card3 - 1 : (int)lastGroup.Cards[0].ComparePoint;
                for (int count = minCount; count <= 8; count++)
                {
                    int mPt = count == minCount ? minPt + 1 : (int)CardPoint.Card3;
                    for (int pt = mPt; pt <= (int)CardPoint.Card2; pt++)
                    {
                        if (stack[pt].Count == count)
                        {
                            List<Card> cards = new List<Card>();
                            for (int i = 0; i < count; i++) cards.Add(stack[pt][i]);
                            result.AddHint(cards.ToArray());
                        }
                    }
                }
                if (stack.Joker.Count >= 4) result.AddHint(new Card[] { stack.Joker[0], stack.Joker[1], stack.Joker[2], stack.Joker[3] });
                return result;
            }
            catch
            {
                return result;
            }
        }
    }
}
